package seismosurfer.servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import seismosurfer.data.MetaData;
import seismosurfer.database.MetaDataDAO;
import seismosurfer.servlets.util.ServletUtil;

/**
 * This servlet shows the applet page
 * if a specific map is requested, otherwise
 * it shows a page with a list of the
 * available maps.
 *
 */
public class MapServlet extends BaseServlet {
    
    private static final long serialVersionUID = 530146721606841047L;

    public static final String MAPS = "maps";

    public static final String SIZE = "size";

    public static final String PATH = "path";

    // Initialize global variables
    public void init() throws ServletException {
    }

    // Process the HTTP Get request
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        MetaDataDAO resInfo = new MetaDataDAO();

        String id = request.getParameter("id");

        if (id != null) {
            int mapID = Integer.parseInt(id);
            String path = resInfo.getResourcePath(mapID);

            if (path == null) {
                ServletUtil.displayUserError(response);
                return;
            }

            request.setAttribute(PATH, path);
            forward("/applet.jsp", request, response);
        } else {
            String maps = getMaps(resInfo);
            String appletSize = ServletUtil.getAppletSize();

            request.setAttribute(MAPS, maps);
            request.setAttribute(SIZE, appletSize);
            forward("/map.jsp", request, response);
        }

    }
    
    /**
     * Converts the available maps` paths to
     * an HTML list.
     * 
     * @param resInfo the MetaDataDAO from which 
     *        the maps data will be retrieved
     * @return the HTML formatted String
     * @throws IOException
     */
    public static String getMaps(MetaDataDAO resInfo) throws IOException {

        int size = 0;
        String description = "";

        List resList = resInfo.getMetaData();
        size = resList.size();

        Collection descColl = new ArrayList();
        MetaData resData = new MetaData();

        for (int i = 0; i < size; i++) {
            resData = (MetaData) resList.get(i);
            if (resData.getResourceCategory().equalsIgnoreCase(MAPS)) {
                description = resData.getResourceDescription();
                descColl.add(description);
            }
        }

        descColl = new HashSet(descColl);

        String maps = "<UL> ";

        for (Iterator i = descColl.iterator(); i.hasNext();) {
            description = (String) i.next();

            boolean found = false;
            for (int k = 0; k < size; k++) {
                resData = (MetaData) resList.get(k);

                if (description.equalsIgnoreCase(resData
                        .getResourceDescription())
                        && (found == false)) {

                    maps += "<LI> " + description + " <A HREF=\"map?id="
                            + resData.getMetaDataID() + " \"> "
                            + resData.getResourceSize() + "</A> ";

                    found = true;
                } else if (description.equalsIgnoreCase(resData
                        .getResourceDescription())
                        && (found == true)) {

                    maps += " <A HREF=\"map?id=" + resData.getMetaDataID()
                            + " \"> " + resData.getResourceSize() + "</A> ";
                }
            }

        }
        maps += "</UL>";

        return maps;
    }

    // Clean up resources
    public void destroy() {

    }
}
